<template>
  <a-button class="float-right" :loading="uploading" @click="exportExcel" type="primary" ghost>导出数据</a-button>
</template>
<script>
import XLSX from 'xlsx'
import { getInterest } from '@/api/common'
export default {
  name: 'interestExport',
  components: {},
  props: ['columns'],
  data: () => ({
    uploading: false,
    currency: ''
  }),
  methods: {
    async exportExcel() {
      this.uploading = true
      /* 从表生成工作簿对象 */
      const form = {
        projectId: this.$route.query.id
      }
      const res = await getInterest(form)
      const data = res.result.data
      this.currency = data.currency
      const _list = data.dict
      const colmuns = []
      for (let key in _list) {
        let interests = 0
        let limitFees = 0
        const innerData = []
        const crrt = _list[key]
        for (let day in crrt) {
          innerData.push({
            month: day,
            currency: this.currency,
            interests: crrt[day].interests,
            limitFees: crrt[day].limitFees
          })
          interests += crrt[day].interests
          limitFees += crrt[day].limitFees
        }
        colmuns.push({
          month: key,
          currency: data.currency,
          interests: interests,
          limitFees: limitFees
        })
        innerData.map(el => {
          colmuns.push(el)
        })
        console.log(colmuns)
      }
      const exportData = colmuns.map((el, i) => {
        el.index = i
        const res = {}
        this.columns.forEach(col => {
          if (el[col.dataIndex]) {
            res[col.title] = el[col.dataIndex]
          }
        })
        return res
      })
      // console.log(exportData)

      var ws = XLSX.utils.json_to_sheet(exportData)
      /* 获取二进制字符串作为输出 */
      var wb = XLSX.utils.book_new() /*新建book*/
      XLSX.utils.book_append_sheet(wb, ws, '利息贷款限额费分摊') /* 生成xlsx文件(book,sheet数据,sheet命名) */
      XLSX.writeFile(wb, '利息贷款限额费分摊列表.xlsx') /*写文件(book,xlsx文件名称)*/
      this.uploading = false
    }
  }
}
</script>
<style lang="css" scoped></style>
